package com.xl6667.util;

import com.xl6667.domain.entity.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

public class SecurityUtil {
    /**
     * 获取Authentication对象
     * @return
     */
    public static Authentication getAuthentication(){
        return SecurityContextHolder.getContext().getAuthentication();
    }

    /**
     * 获取用户loginUser
     * @return
     */
    public static LoginUser getLoginUser(){
        return (LoginUser) getAuthentication().getPrincipal();
    }

    /**
     * 获取登录用户Id
     * @return
     */
    public static Long getUserId(){
       return getLoginUser().getUser().getId();
    }

    /**
     * 获取用户类型d
     *
     * @return
     */
    public static String getUserType(){
        return getLoginUser().getUser().getType();

    }

    public static boolean isAdmin() {
        Long id = getUserId();
        return id!=null && id.equals(1L);
    }
}
